<?php
class ContactController extends ContactAppController {
	var $uses = array('User');
	var $view = null;
	var $theme = null;
	
	public function send(){	
//		Configure::write('debug', 0);
		
		$this->autoRender = false;
// 		if($this->Session->read('Captcha') == $this->data['Contact']['captcha']){
			if($this->_send()){
				$this->Session->setFlash(__("Message sent.", true));
			}else{
				$this->Session->setFlash(__("Error when sending messages. Please try to send.", true));
			}
// 		}else{
// 			$this->Session->setFlash(__("Please re-enter the correct numbers.", true));;
// 		}
		$this->redirect($this->referer());
	}
	
    private function _send(){
    	if(!isset($this->params['named']['mailto'])){
    	$manag = $this->User->find('list',array('fields'=>'email','conditions'=>array('User.group'=>"manag"))); // moderators
    	if(is_array($manag) && !empty($manag)){
    	}else{
    		$manag = $this->User->find('list',array('fields'=>'email','conditions'=>array('User.group'=>"admin")));
    	}
    	}else{
    		$manag=array(0=>$this->params['named']['mailto']);
    	}
//    	$this->Email->delivery = 'debug';
        $this->Email->to = $manag; 
        $this->Email->subject = __("Message ",true).$_SERVER['HTTP_HOST']; 
//        $this->Email->replyTo = $manag; 
        $this->Email->from = $_SERVER['HTTP_HOST']." <{$_SERVER['HTTP_HOST']}>"; 
        $this->Email->template = "contact-from-www"; 
        $this->Email->sendAs = 'both'; // text; html; both;
 			
        //Do not pass any args to send() 
        if ( $this->Email->send() ) { 
             return true;
        } else { 
            return false; 
        }     
    }
}